package net.anzhuo.controller.db.report;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;
import net.anzhuo.common.entity.PaginationVO;
import net.anzhuo.common.utils.RedisUtil;
import net.anzhuo.entity.GsBase;
import net.anzhuo.entity.GsContactInfo;

import net.anzhuo.service.GsBaseService;
import net.anzhuo.service.GsContactInfoService;

@RestController
@Slf4j
public class WebInfoController {

	@Autowired
	private GsContactInfoService gsContactInfoService;

	@Autowired
	private GsBaseService gsBaseService;
	
	@RequestMapping("/WebSiteV4/GetCompanyWebSite")
	public net.anzhuo.entity.db.companywebsite.ResponseResult GetCompanyWebSite(String searchKey, String pageSize) {
		log.info("===>GetCompanyWebSite_searchKey="+searchKey+", pageSize="+pageSize);
		long startTime=System.currentTimeMillis();
		if (StringUtils.isBlank(pageSize)) {
			pageSize = "50";
		}
		
		net.anzhuo.entity.db.companywebsite.ResponseResult rr = null;
		try {
			rr = (net.anzhuo.entity.db.companywebsite.ResponseResult)RedisUtil.get("GetCompanyWebSite:"+searchKey);
			if (rr != null) {
				 long endTime=System.currentTimeMillis();
				 float excTime=(float)(endTime-startTime)/1000;
				 log.info(" GetCompanyWebSite 执行时间："+excTime+"s");
				return rr;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		GsContactInfo gsZhuanliQuery = new GsContactInfo();
	    GsBase base=gsBaseService.getByMd5(searchKey);
		gsZhuanliQuery.setCoMd5(searchKey);
		gsZhuanliQuery.setPageSize(Integer.parseInt(pageSize));
		PaginationVO<GsContactInfo> zhuanliList = gsContactInfoService.getListWithPage(gsZhuanliQuery);
		
		net.anzhuo.entity.db.companywebsite.ResponseResult responseResult = new net.anzhuo.entity.db.companywebsite.ResponseResult();
		responseResult.setStatus("200");
		responseResult.setMessage("查询成功");
		
		net.anzhuo.entity.db.companywebsite.Paging paging = new 	net.anzhuo.entity.db.companywebsite.Paging();
		paging.setPageIndex(1);
		paging.setPageSize(Integer.parseInt(pageSize));
		paging.setTotalRecords(zhuanliList.getTotalCount());
		responseResult.setPaging(paging);
		
		List<net.anzhuo.entity.db.companywebsite.Result> resultList = new ArrayList<>();
		net.anzhuo.entity.db.companywebsite.Result result = null;
		if (zhuanliList.getList()!=null && zhuanliList.getList().size()>0) {
			for (GsContactInfo z : zhuanliList.getList()) {
				result = new net.anzhuo.entity.db.companywebsite.Result();
			    result.setCompanyName(base.getName());
			    result.setDomains(z.getDomain());
			    result.setHomeSite(z.getWebindex());
			    result.setKeyNo(z.getMd5());
			    result.setRecordNo(z.getRecordnumber());
			    result.setSDate(z.getChecktime());
			    result.setStatus(z.getState());
			    result.setTypeName(z.getNature());
			    result.setTitle(z.getWebname());
				resultList.add(result);
			}
		}
		responseResult.setResult(resultList);
		
		try {
			RedisUtil.set("GetCompanyWebSite:"+searchKey, responseResult);
		} catch (Exception e) {
			e.printStackTrace();
		}
		 long endTime=System.currentTimeMillis();
		 float excTime=(float)(endTime-startTime)/1000;
		 log.info(" GetCompanyWebSite 执行时间："+excTime+"s");
		return responseResult;
	}
}
